package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.guns.modular.wms.model.params.BrowseParam;
import cn.stylefeng.guns.modular.wms.model.params.CommunicateParam;
import cn.stylefeng.guns.modular.wms.model.result.BrowseResult;
import cn.stylefeng.guns.modular.wms.model.result.CommunicateResult;
import cn.stylefeng.guns.modular.wms.service.BrowseService;
import cn.stylefeng.guns.modular.wms.service.CommunicateService;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 *沟通记录模块管理
 */
@Controller
@RequestMapping("/api/comm")
@Api(tags = "沟通记录模块管理")
public class ApiCommController extends BaseController {

    @Autowired
    private CommunicateService communicateService;
    @Resource
    private MemberService memberService;

    /**
     * 查询沟通列表
     */
    @ResponseBody
    @RequestMapping(value = "/getCommList",method = RequestMethod.GET)
    @ApiOperation(value = "查询沟通列表", notes = "查询沟通列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    public ResponseData getCommList(CommunicateParam param) {
        try{
            long memberId=MemberTokenUtil.getMemberId();
            Member member=memberService.getById(memberId);
            if(param.getType()==0){//用户端
                param.setMemberId(member.getMemberSn());
            }else{//企业端
                param.setComMemberId(member.getMemberSn());
            }
            LayuiPageInfo pageInfo= communicateService.findPageBySpec(param);
            return ResponseData.success(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 查询沟通详情
     */
    @ResponseBody
    @RequestMapping(value = "/getCommDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询沟通详情", notes = "查询沟通详情")
    public ResponseData getCommDetail(CommunicateParam param) {
        CommunicateResult result=communicateService.findBySpec(param);
        Map<String,Object> map=new HashMap<>();
        map.put("data",result);
        return ResponseData.success(map);
    }

    /**
     * 添加/更新沟通
     */
    @ResponseBody
    @RequestMapping(value = "/addOrUpdateComm",method = RequestMethod.POST)
    @ApiOperation(value = "添加/更新沟通", notes = "添加/更新沟通")
    public ResponseData addOrUpdateComm(@RequestBody CommunicateParam param) {
        try {
            communicateService.update(param);
            return ResponseData.success();
        }catch (Exception e){
            return ResponseData.error(e.getMessage());
        }
    }

    /**
     * 删除简历沟通
     */
    @ResponseBody
    @RequestMapping(value = "/deleteComm",method = RequestMethod.POST)
    @ApiOperation(value = "删除沟通", notes = "删除沟通")
    public ResponseData deleteComm(@RequestBody CommunicateParam param) {
        try {
            communicateService.removeById(param.getId());
            return ResponseData.success();
        }catch (Exception e){
            return ResponseData.error(e.getMessage());
        }
    }

}



